<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="clickShowAdd"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 作用域插槽获取当前角色对象，row就是角色对象 -->
                <template #default="{row}">
                  <el-button size="small" type="success" @click="openDialog(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDelList(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[2, 3, 5, 10]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 弹出 -->
        </el-tabs>
        <!-- 添加 -->
        <el-dialog title="添加角色" :visible="isShow" @close="closeDialog()">
          <el-form ref="roleForm" :model="form" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入角色名称" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="form.description" placeholder="请输入角色描述" />
            </el-form-item>
          </el-form>

          <template #footer>
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="handleAddform">确认</el-button>
          </template>
        </el-dialog>
        <!-- 编辑 -->
        <el-dialog title="编辑角色" :visible="isShow1" @close="closeDialog()">
          <el-form ref="roleForm" :model="form" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入角色名称" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="form.description" placeholder="请输入角色描述" />
            </el-form-item>
          </el-form>

          <template #footer>
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="handleDirList()">确认</el-button>
          </template>
        </el-dialog>
        <!-- 分配权限的弹层 -->
        <el-dialog title="分配权限" :visible="isShowshow" @close="isShowshow=false">
          <!-- tree组件选中的节点，有2种方式
              1.通过node(节点对象)方式
              2.通过key的方式，需要给tree添加node-key属性
            代码中:可以通过this.$refs.tree获取到tree组件对象，调用方法事件
          1.获取getCheckedNodes()/getCheckedKeys()
          1.设置setCheckedNodes([])/setCheckedKeys([])
        -->
          <el-tree ref="tree" :data="permList" show-checkbox :props="{label:'name'}" default-expand-all check-strictly node-key="id" />
          <template #footer>
            <div style="text-align: right;">
              <el-button @click="isShowshow=false">取消</el-button>
              <el-button type="primary" @click="handleAssign">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getSettingAPI, addSettingAPI, delSettingAPI, dirSettingAPI, reqGetRoleDetail, getComAPI, assignAPI } from '@/api/setting'
import { getPermissionAPI } from '@/api/permission'
import { list2Tree } from '@/utils/shift'
export default {
  name: 'SettingsView',
  data() {
    return {
      activeName: 'role',
      // 角色列表
      list: [],
      // 数据总条数
      total: 0,
      // 查询参数
      query: {
        page: 1,
        pagesize: 10
      },
      // 弹框显示隐藏
      isShow: false,
      isShow1: false,
      isShowshow: false,
      form: {
        name: '',
        description: '',
        id: ''
      },
      companyForm: {},
      permList: []
    }
  },
  created() {
    this.getSettingList()
    this.getComInfo()
  },
  methods: {
    // 分配权限
    async handleAssign(id) {
      // 获取树形组件选中的节点
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignAPI(this.roleid, permIds)
      this.isShowshow = false
      this.$message.success('分配权限成功')
    },
    async getPermission() {
      // 发请求获取列表
      const { data } = await getPermissionAPI()
      // 列表转树形
      this.permList = list2Tree(data, '0')
    },
    async openDialog(roleid) {
      this.roleid = roleid
      this.isShowshow = true
      this.getPermission()
      const resp = await reqGetRoleDetail(roleid)
      this.$refs.tree.setCheckedKeys(resp.data.permIds)
    },
    // 获取员工列表
    async getSettingList() {
      // 开始加载
      this.loading = true
      // 获取角色信息数据
      const { data } = await getSettingAPI(this.query)
      // console.log(data)
      // 解构出数组
      const { rows, total } = data
      // 赋值给自定义的list
      this.list = rows
      // 总条数
      this.total = total
      // 关闭加载
      this.loading = false
    },
    // 获取公司信息
    async getComInfo() {
      const { data } = await getComAPI(this.$store.state.user.userInfo.companyId)
      this.companyForm = data
    },
    // 点击x关闭弹框
    closeDialog() {
      this.isShow = false
      this.isShow1 = false
      this.form = {
        name: '',
        description: '',
        id: ''
      }
    },
    // 点击新增显示弹框
    clickShowAdd() {
      this.isShow = true
    },
    // 点击编辑显示弹框
    async showEdit(id) {
      const resp = await reqGetRoleDetail(id)
      console.log(resp)
      this.form = resp.data
      // console.log(this.form)
      this.isShow1 = true
    },
    // 点击取消关闭弹框
    btnCancel() {
      this.isShow = false
      this.isShow1 = false
      this.form = {
        name: '',
        description: '',
        id: '',
        permIds: ''
      }
    },
    // 添加角色信息
    async handleAddform() {
      await addSettingAPI(this.form)
      this.getSettingList()
      this.isShow = false
    },
    // 删除角色信息
    handleDelList(id) {
      delSettingAPI(id)
      // 如果当前页面大于1，并且list只有1条数据，获取上一页数据
      if (this.query.page > 1 && this.list.length === 1) {
        this.query.page--
      }
      this.isShow = false
      this.getSettingList()
      this.$message.success('删除成功')
    },
    // 编辑角色信息
    handleDirList() {
      dirSettingAPI(this.form)
      this.isShow1 = false
      this.getSettingList()
    },
    handleSizeChange(val) {
      // 更新每页条数
      this.query.pagesize = val
      // 重置当前页
      this.query.page = 1
      // 调用方法, 重新请求
      this.getSettingList()
    },
    // 处理页码变化
    handleCurrentChange(val) {
      this.query.page = val
      this.getSettingList()
    },
    // 自定义下标的方法
    indexMethod(index) {
      // 下标的规律是当前页数据的序号+每页条数*（当前页码-1）
      return (this.query.page - 1) * this.query.pagesize + index + 1
    }
  }
}
</script>

    <style>

    </style>

